package com.edais.service.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.edais.actions.PerMatchDetailAction;
import com.edais.dao.FinanceDao;
import com.edais.domain.Finance;
import com.edais.service.WalletAssignService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.JsonUtil;
import com.edais.util.Message;

@Service

@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
public class WalletAssignServiceImp implements WalletAssignService {
	/** 日志 */
	private static final Log logger = LogFactory.getLog(PerMatchDetailAction.class);
	
	/** 理财计划 */
	@Autowired
	private FinanceDao financeDao;

	/**
	 * 获取涨薪宝系列下可转让债权列表
	 * @param 
	 */
	@Override
	public Map<String, Object> getAssignList(Map<String, Object> map) throws Exception {
		Map<String, Object> rtnMap = new HashMap<String, Object>();

		String transCode = "E330011";

		// 构建json字符串
		String jsonStr = CommonUtil.constructJsonStr(transCode, (String) map.get("userId"), map);
		
		// 调用核心接口
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		// rescode = "00000"
		if(Consts.SUCCESS_CODE.equals(jsonObject.getString("rescode"))){
			JSONArray jsonArr = jsonObject.getJSONArray("list");
			if(jsonArr != null && jsonArr.size() > 0){
				List<Map<String, Object>> list = JsonUtil.toList(jsonArr);
				for (int i = 0; i < list.size(); i++) {
					Map<String, Object> tmpMap = list.get(i);
					//tmpMap.put("creditState", "回款中");     // 设置交易状态 2.回款中
					tmpMap.put("creditState", this.getCreditState((String) tmpMap.get("creditState")));
					list.set(i, tmpMap);
				}
				rtnMap.put("list", list);
				rtnMap.put("listSize", list.size());
			} else {
				rtnMap.put("list", new ArrayList<Object>());
				rtnMap.put("listSize", "0");
			}
			// 总记录数
			rtnMap.put("totalNum", jsonObject.getString("totalNum"));
			
			// 设置返回code
			rtnMap.put("rescode", Consts.SUCCESS_CODE);
			rtnMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
			rtnMap.put("resmsg_cn", "");
		} else {
			logger.debug("=====获取涨薪宝系列下可转让债权列表 核心接口调用异常=====");
			logger.debug("核心接口：" + transCode);
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			rtnMap.put("rescode", Consts.ERROR_CODE);
			rtnMap.put("resmsg", Consts.ErrorMsg.MSG00001);
			rtnMap.put("resmsg_cn", Message.CnDescriptionExpression.BUSINESSE_ERROR);
		}

		return rtnMap;
	}

	/**
	 * 获取涨薪宝系列下转让中债权列表
	 * @param 
	 */
	@Override
	public Map<String, Object> getInAssignList(Map<String, Object> map) throws Exception {
		Map<String, Object> rtnMap = new HashMap<String, Object>();

		String transCode = "E330012";

		// 构建json字符串
		String jsonStr = CommonUtil.constructJsonStr(transCode, (String) map.get("userId"), map);
		
		// 调用核心接口
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		// rescode = "00000"
		if(Consts.SUCCESS_CODE.equals(jsonObject.getString("rescode"))){
			JSONArray jsonArr = jsonObject.getJSONArray("list");
			if(jsonArr != null && jsonArr.size() > 0){
				List<Map<String, Object>> list = JsonUtil.toList(jsonArr);
				for (int i = 0; i < list.size(); i++) {
					Map<String, Object> tmpMap = list.get(i);
					//tmpMap.put("creditState", "转让中");     // 设置交易状态
					tmpMap.put("creditState", this.getCreditState((String) tmpMap.get("creditState")));
					list.set(i, tmpMap);
				}
				
				rtnMap.put("list", list);
				rtnMap.put("listSize", list.size());
				
			} else {
				rtnMap.put("list", new ArrayList<Object>());
				rtnMap.put("listSize", "0");
			}
			// 总记录数
			rtnMap.put("totalNum", jsonObject.getString("totalNum"));
			// 债权价值总计
			rtnMap.put("totalCreditValue", jsonObject.getString("totalCreditValue"));
			
			// 设置返回code
			rtnMap.put("rescode", Consts.SUCCESS_CODE);
			rtnMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
			rtnMap.put("resmsg_cn", "");
		} else {
			logger.debug("=====获取涨薪宝系列下可转让债权列表 核心接口调用异常=====");
			logger.debug("核心接口：" + transCode);
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			rtnMap.put("rescode", Consts.ERROR_CODE);
			rtnMap.put("resmsg", Consts.ErrorMsg.MSG00001);
			rtnMap.put("resmsg_cn", Message.CnDescriptionExpression.BUSINESSE_ERROR);
		}

		return rtnMap;
	}

	/**
	 * 获取涨薪宝系列下已转让债权列表
	 * @param 
	 */
	@Override
	public Map<String, Object> getAssignCompList(Map<String, Object> map) throws Exception {
		Map<String, Object> rtnMap = new HashMap<String, Object>();

		String transCode = "E330013";

		// 构建json字符串
		String jsonStr = CommonUtil.constructJsonStr(transCode, (String) map.get("userId"), map);
		
		// 调用核心接口
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		// rescode = "00000"
		if(Consts.SUCCESS_CODE.equals(jsonObject.getString("rescode"))){
			JSONArray jsonArr = jsonObject.getJSONArray("list");
			if(jsonArr != null && jsonArr.size() > 0){
				List<Map<String, Object>> list = JsonUtil.toList(jsonArr);
				for (int i = 0; i < list.size(); i++) {
					Map<String, Object> tmpMap = list.get(i);
					//tmpMap.put("creditState", "已转让");     // 设置交易状态
					tmpMap.put("creditState", this.getCreditState((String) tmpMap.get("creditState")));
					list.set(i, tmpMap);
				}
				
				rtnMap.put("list", list);
				rtnMap.put("listSize", list.size());
				
			} else {
				rtnMap.put("list", new ArrayList<Object>());
				rtnMap.put("listSize", "0");
			}
			//总记录数
			rtnMap.put("totalNum", jsonObject.getString("totalNum"));
			// 债权价值总计
			rtnMap.put("totalCreditValue", jsonObject.getString("totalCreditValue"));
			
			// 设置返回code
			rtnMap.put("rescode", Consts.SUCCESS_CODE);
			rtnMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
			rtnMap.put("resmsg_cn", "");
		} else {
			logger.debug("=====获取涨薪宝系列下可转让债权列表 核心接口调用异常=====");
			logger.debug("核心接口：" + transCode);
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			rtnMap.put("rescode", Consts.ERROR_CODE);
			rtnMap.put("resmsg", Consts.ErrorMsg.MSG00001);
			rtnMap.put("resmsg_cn", Message.CnDescriptionExpression.BUSINESSE_ERROR);
		}

		return rtnMap;
	}

	//TODO 待根据产品系列获取产品列表上线
	/**
	 * 获取涨薪宝系列下产品
	 */
	@Override
	public String getWalletFinanceString() {
		StringBuffer financeString = new StringBuffer();

		List<Finance> financeList = financeDao.getFinanceListByQueryFlag("2");
		// 设置产品类型
		if (financeList == null || financeList.size() == 0) {
			financeString.append("");
		} else {
			for (int i = 0; i < financeList.size(); i++) {
				Finance finance = financeList.get(i);
				financeString.append(finance.getId());
				
				if (i != financeList.size() - 1) {
					financeString.append(", ");
				}
			}
		}
		return financeString.toString();
	}
	
	//TODO（后期可提至属性文件中）
	/***
	 * 获取债权状态类型
	 *
	 * @return
	 */
	private String getCreditState(String creditType){
		HashMap<String, String> hashMap = new HashMap<String, String>();
		// 债权交易状态:2.可转让;7.转让中;6.已转让
		hashMap.put("2", "回款中");//可转让--回款中
		hashMap.put("7", "转让中");
		hashMap.put("6", "已转让");
		return (String) hashMap.get(creditType);
	}
}
